const MySQLDB = require('mysql');
const logger = require('./logger')
const MySQL = {
  connect: (connectInfo) => {
    return new Promise(function (resolve, reject) {
      const mysqlConfig = MySQLDB.createConnection(connectInfo);
      mysqlConfig.connect((err) => {
        if (err) {
          logger.error(`MySQL`, `connect`, `error`, err)
          reject(err)
        };
        resolve(mysqlConfig);
      })
    });
  },
  query: (conn, query) => {
    conn.query(query, function (error, results, fields) {
      if (error) {
        throw error
      } else {
        results
      }
    });
  },
  close: conn => {
    return new Promise(function (resolve, reject) {
      conn.end(err => {
        if (err) {
          console.error(err.message);
          logger.error(`MySQL`, `close`, `error`, err)
          reject(err)
        }
        resolve();
      });
    })
  }
}

module.exports = {
  MySQL
};